﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="WxCommon.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：WxCommon.cs
// 创建时间：2014-12-25 12:59
// 修改时间：2014-12-23 17:06
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using WebCommon.Com;

namespace Payment.Weixin
{
    #region

    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    using WebCommon;

    #endregion

    /// <summary>
    /// The wx common.
    /// </summary>
    public class WxCommon
    {
        #region Public Methods and Operators

        /// <summary>
        /// 获取paternerKey
        /// </summary>
        /// <param name="dictionary">
        /// The dictionary.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string GetOrderBy(IEnumerable<KeyValuePair<string, string>> dictionary )
        {
            var dy = dictionary.OrderBy(x => x.Key).ToDictionary(x => x.Key, y => y.Value);

            List<string> strings = new List<string>();
            foreach (var item in dy)
            {
                strings.Add(item.Key + "=" + item.Value);
            }

            string join = string.Join("&", strings);

            return join;
        }

        /// <summary>
        /// The get string sign temp.
        /// </summary>
        /// <param name="str">
        /// The str.
        /// </param>
        /// <param name="paternerKey">
        /// The paterner key.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string GetStringSignTemp(string str, string paternerKey)
        {
            var md5 = str + "&key=" + paternerKey;
            return Encryption.Md532(md5).ToUpper();
        }

        /// <summary>
        /// The get urlencode.
        /// </summary>
        /// <param name="dictionary">
        /// The dictionary.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public static string GetUrlencode(Dictionary<string, string> dictionary)
        {
            var dy = dictionary.OrderBy(x => x.Key).ToDictionary(x => x.Key, y => y.Value);

            List<string> strings = new List<string>();
            foreach (var item in dy)
            {
                strings.Add(item.Key + "=" + Static.UrlencodeToUpper(item.Value));
            }

            string join = string.Join("&", strings);

            return join;
        }

        #endregion
    }
}